/*******************************************************************************
  PIO PLIB

  Company:
    Microchip Technology Inc.

  File Name:
    plib_pio.h

  Summary:
    PIO PLIB Header File

  Description:
    This library provides an interface to control and interact with Parallel
    Input/Output controller (PIO) module.

*******************************************************************************/

/*******************************************************************************
* Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries.
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/

#ifndef PLIB_PIO_H
#define PLIB_PIO_H

#include "device.h"
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

// DOM-IGNORE-BEGIN
#ifdef __cplusplus  // Provide C++ Compatibility

    extern "C" {

#endif
// DOM-IGNORE-END

// *****************************************************************************
// *****************************************************************************
// Section: Data types and constants
// *****************************************************************************
// *****************************************************************************


/*** Macros for BUZZER_PWM pin ***/
#define BUZZER_PWM_Get()               ((PIOB_REGS->PIO_PDSR >> 12U) & 0x1U)
#define BUZZER_PWM_PIN                  PIO_PIN_PB12

/*** Macros for DRV_TOUCH_PIN_INT pin ***/
#define DRV_TOUCH_PIN_INT_Set()               (PIOB_REGS->PIO_SODR = ((uint32_t)1U<<0U))
#define DRV_TOUCH_PIN_INT_Clear()             (PIOB_REGS->PIO_CODR = ((uint32_t)1U<<0U))
#define DRV_TOUCH_PIN_INT_Toggle()            (PIOB_REGS->PIO_ODSR ^= ((uint32_t)1U<<0U))
#define DRV_TOUCH_PIN_INT_OutputEnable()      (PIOB_REGS->PIO_OER = ((uint32_t)1U<<0U))
#define DRV_TOUCH_PIN_INT_InputEnable()       (PIOB_REGS->PIO_ODR = ((uint32_t)1U<<0U))
#define DRV_TOUCH_PIN_INT_Get()               ((PIOB_REGS->PIO_PDSR >> 0U) & 0x1U)
#define DRV_TOUCH_PIN_INT_PIN                  PIO_PIN_PB0

/*** Macros for GPIO_PA13 pin ***/
#define GPIO_PA13_Set()               (PIOA_REGS->PIO_SODR = ((uint32_t)1U<<13U))
#define GPIO_PA13_Clear()             (PIOA_REGS->PIO_CODR = ((uint32_t)1U<<13U))
#define GPIO_PA13_Toggle()            (PIOA_REGS->PIO_ODSR ^= ((uint32_t)1U<<13U))
#define GPIO_PA13_OutputEnable()      (PIOA_REGS->PIO_OER = ((uint32_t)1U<<13U))
#define GPIO_PA13_InputEnable()       (PIOA_REGS->PIO_ODR = ((uint32_t)1U<<13U))
#define GPIO_PA13_Get()               ((PIOA_REGS->PIO_PDSR >> 13U) & 0x1U)
#define GPIO_PA13_PIN                  PIO_PIN_PA13
#define GPIO_PA13_IDR()               (PIOA_REGS->PIO_IDR = ((uint32_t)1U<<13U))
#define GPIO_PA13_PPUER()             (PIOA_REGS->PIO_PUER = ((uint32_t)1U<<13U))
#define GPIO_PA13_PER()               (PIOA_REGS->PIO_PER = ((uint32_t)1U<<13U))

/*** Macros for GPIO_PA14 pin ***/
#define GPIO_PA14_Set()               (PIOA_REGS->PIO_SODR = ((uint32_t)1U<<14U))
#define GPIO_PA14_Clear()             (PIOA_REGS->PIO_CODR = ((uint32_t)1U<<14U))
#define GPIO_PA14_Toggle()            (PIOA_REGS->PIO_ODSR ^= ((uint32_t)1U<<14U))
#define GPIO_PA14_OutputEnable()      (PIOA_REGS->PIO_OER = ((uint32_t)1U<<14U))
#define GPIO_PA14_InputEnable()       (PIOA_REGS->PIO_ODR = ((uint32_t)1U<<14U))
#define GPIO_PA14_Get()               ((PIOA_REGS->PIO_PDSR >> 14U) & 0x1U)
#define GPIO_PA14_PIN                  PIO_PIN_PA14
#define GPIO_PA14_IDR()               (PIOA_REGS->PIO_IDR = ((uint32_t)1U<<14U))
#define GPIO_PA14_PPUER()             (PIOA_REGS->PIO_PUER = ((uint32_t)1U<<14U))
#define GPIO_PA14_PER()               (PIOA_REGS->PIO_PER = ((uint32_t)1U<<14U))

/*** Macros for GPIO_PA12 pin ***/
#define GPIO_PA12_Set()               (PIOA_REGS->PIO_SODR = ((uint32_t)1U<<12U))
#define GPIO_PA12_Clear()             (PIOA_REGS->PIO_CODR = ((uint32_t)1U<<12U))
#define GPIO_PA12_Toggle()            (PIOA_REGS->PIO_ODSR ^= ((uint32_t)1U<<12U))
#define GPIO_PA12_OutputEnable()      (PIOA_REGS->PIO_OER = ((uint32_t)1U<<12U))
#define GPIO_PA12_InputEnable()       (PIOA_REGS->PIO_ODR = ((uint32_t)1U<<12U))
#define GPIO_PA12_Get()               ((PIOA_REGS->PIO_PDSR >> 12U) & 0x1U)
#define GPIO_PA12_PIN                  PIO_PIN_PA12
#define GPIO_PA12_IDR()               (PIOA_REGS->PIO_IDR = ((uint32_t)1U<<12U))
#define GPIO_PA12_PPUER()             (PIOA_REGS->PIO_PUER = ((uint32_t)1U<<12U))
#define GPIO_PA12_PER()               (PIOA_REGS->PIO_PER = ((uint32_t)1U<<12U))

/*** Macros for HTR_EN pin ***/
#define HTR_EN_Set()               (PIOA_REGS->PIO_SODR = ((uint32_t)1U<<4U))
#define HTR_EN_Clear()             (PIOA_REGS->PIO_CODR = ((uint32_t)1U<<4U))
#define HTR_EN_Toggle()            (PIOA_REGS->PIO_ODSR ^= ((uint32_t)1U<<4U))
#define HTR_EN_OutputEnable()      (PIOA_REGS->PIO_OER = ((uint32_t)1U<<4U))
#define HTR_EN_InputEnable()       (PIOA_REGS->PIO_ODR = ((uint32_t)1U<<4U))
#define HTR_EN_Get()               ((PIOA_REGS->PIO_PDSR >> 4U) & 0x1U)
#define HTR_EN_PIN                  PIO_PIN_PA4

/*** Macros for HTR_SDA pin ***/
#define HTR_SDA_Get()               ((PIOA_REGS->PIO_PDSR >> 7U) & 0x1U)
#define HTR_SDA_PIN                  PIO_PIN_PA7

/*** Macros for HTR_SCL pin ***/
#define HTR_SCL_Get()               ((PIOA_REGS->PIO_PDSR >> 8U) & 0x1U)
#define HTR_SCL_PIN                  PIO_PIN_PA8

/*** Macros for DRV_TOUCH_PIN_RESET pin ***/
#define DRV_TOUCH_PIN_RESET_Set()               (PIOC_REGS->PIO_SODR = ((uint32_t)1U<<25U))
#define DRV_TOUCH_PIN_RESET_Clear()             (PIOC_REGS->PIO_CODR = ((uint32_t)1U<<25U))
#define DRV_TOUCH_PIN_RESET_Toggle()            (PIOC_REGS->PIO_ODSR ^= ((uint32_t)1U<<25U))
#define DRV_TOUCH_PIN_RESET_OutputEnable()      (PIOC_REGS->PIO_OER = ((uint32_t)1U<<25U))
#define DRV_TOUCH_PIN_RESET_InputEnable()       (PIOC_REGS->PIO_ODR = ((uint32_t)1U<<25U))
#define DRV_TOUCH_PIN_RESET_Get()               ((PIOC_REGS->PIO_PDSR >> 25U) & 0x1U)
#define DRV_TOUCH_PIN_RESET_PIN                  PIO_PIN_PC25

/*** Macros for GPIO_PC31 pin ***/
#define GPIO_PC31_Set()               (PIOC_REGS->PIO_SODR = ((uint32_t)1U<<31U))
#define GPIO_PC31_Clear()             (PIOC_REGS->PIO_CODR = ((uint32_t)1U<<31U))
#define GPIO_PC31_Toggle()            (PIOC_REGS->PIO_ODSR ^= ((uint32_t)1U<<31U))
#define GPIO_PC31_OutputEnable()      (PIOC_REGS->PIO_OER = ((uint32_t)1U<<31U))
#define GPIO_PC31_InputEnable()       (PIOC_REGS->PIO_ODR = ((uint32_t)1U<<31U))
#define GPIO_PC31_Get()               ((PIOC_REGS->PIO_PDSR >> 31U) & 0x1U)
#define GPIO_PC31_PIN                  PIO_PIN_PC31
#define GPIO_PC31_IDR()               (PIOA_REGS->PIO_IDR = ((uint32_t)1U<<31U))
#define GPIO_PC31_PPUER()             (PIOA_REGS->PIO_PUER = ((uint32_t)1U<<31U))
#define GPIO_PC31_PER()               (PIOA_REGS->PIO_PER = ((uint32_t)1U<<31U))


// *****************************************************************************
/* PIO Port

  Summary:
    Identifies the available PIO Ports.

  Description:
    This enumeration identifies the available PIO Ports.

  Remarks:
    The caller should not rely on the specific numbers assigned to any of
    these values as they may change from one processor to the next.

    Not all ports are available on all devices.  Refer to the specific
    device data sheet to determine which ports are supported.
*/


#define    PIO_PORT_A       (PIOA_BASE_ADDRESS)
#define    PIO_PORT_B       (PIOB_BASE_ADDRESS)
#define     PIO_PORT_C      (PIOC_BASE_ADDRESS)
#define     PIO_PORT_D      (PIOD_BASE_ADDRESS)
typedef uint32_t PIO_PORT;

// *****************************************************************************
/* PIO Port Pins

  Summary:
    Identifies the available PIO port pins.

  Description:
    This enumeration identifies the available PIO port pins.

  Remarks:
    The caller should not rely on the specific numbers assigned to any of
    these values as they may change from one processor to the next.

    Not all pins are available on all devices.  Refer to the specific
    device data sheet to determine which pins are supported.
*/

#define    PIO_PIN_PA0     (0U)
#define    PIO_PIN_PA1     (1U)
#define    PIO_PIN_PA2     (2U)
#define    PIO_PIN_PA3     (3U)
#define    PIO_PIN_PA4     (4U)
#define    PIO_PIN_PA5     (5U)
#define    PIO_PIN_PA6     (6U)
#define    PIO_PIN_PA7     (7U)
#define    PIO_PIN_PA8     (8U)
#define    PIO_PIN_PA9     (9U)
#define    PIO_PIN_PA10     (10U)
#define    PIO_PIN_PA11     (11U)
#define    PIO_PIN_PA12     (12U)
#define    PIO_PIN_PA13     (13U)
#define    PIO_PIN_PA14     (14U)
#define    PIO_PIN_PA15     (15U)
#define    PIO_PIN_PA16     (16U)
#define    PIO_PIN_PA17     (17U)
#define    PIO_PIN_PA18     (18U)
#define    PIO_PIN_PA19     (19U)
#define    PIO_PIN_PA20     (20U)
#define    PIO_PIN_PA21     (21U)
#define    PIO_PIN_PA22     (22U)
#define    PIO_PIN_PA23     (23U)
#define    PIO_PIN_PA24     (24U)
#define    PIO_PIN_PA25     (25U)
#define    PIO_PIN_PA26     (26U)
#define    PIO_PIN_PA27     (27U)
#define    PIO_PIN_PA28     (28U)
#define    PIO_PIN_PA29     (29U)
#define    PIO_PIN_PA30     (30U)
#define    PIO_PIN_PA31     (31U)
#define    PIO_PIN_PB0     (32U)
#define    PIO_PIN_PB1     (33U)
#define    PIO_PIN_PB2     (34U)
#define    PIO_PIN_PB3     (35U)
#define    PIO_PIN_PB4     (36U)
#define    PIO_PIN_PB5     (37U)
#define    PIO_PIN_PB6     (38U)
#define    PIO_PIN_PB7     (39U)
#define    PIO_PIN_PB8     (40U)
#define    PIO_PIN_PB9     (41U)
#define    PIO_PIN_PB10     (42U)
#define    PIO_PIN_PB11     (43U)
#define    PIO_PIN_PB12     (44U)
#define    PIO_PIN_PB13     (45U)
#define    PIO_PIN_PB14     (46U)
#define    PIO_PIN_PB15     (47U)
#define    PIO_PIN_PB16     (48U)
#define    PIO_PIN_PB17     (49U)
#define    PIO_PIN_PB18     (50U)
#define    PIO_PIN_PB19     (51U)
#define    PIO_PIN_PB20     (52U)
#define    PIO_PIN_PB21     (53U)
#define    PIO_PIN_PB22     (54U)
#define    PIO_PIN_PB23     (55U)
#define    PIO_PIN_PB24     (56U)
#define    PIO_PIN_PB25     (57U)
#define    PIO_PIN_PC0     (64U)
#define    PIO_PIN_PC1     (65U)
#define    PIO_PIN_PC2     (66U)
#define    PIO_PIN_PC3     (67U)
#define    PIO_PIN_PC4     (68U)
#define    PIO_PIN_PC5     (69U)
#define    PIO_PIN_PC6     (70U)
#define    PIO_PIN_PC7     (71U)
#define    PIO_PIN_PC8     (72U)
#define    PIO_PIN_PC9     (73U)
#define    PIO_PIN_PC10     (74U)
#define    PIO_PIN_PC11     (75U)
#define    PIO_PIN_PC12     (76U)
#define    PIO_PIN_PC13     (77U)
#define    PIO_PIN_PC14     (78U)
#define    PIO_PIN_PC15     (79U)
#define    PIO_PIN_PC16     (80U)
#define    PIO_PIN_PC17     (81U)
#define    PIO_PIN_PC18     (82U)
#define    PIO_PIN_PC19     (83U)
#define    PIO_PIN_PC20     (84U)
#define    PIO_PIN_PC21     (85U)
#define    PIO_PIN_PC22     (86U)
#define    PIO_PIN_PC23     (87U)
#define    PIO_PIN_PC24     (88U)
#define    PIO_PIN_PC25     (89U)
#define    PIO_PIN_PC26     (90U)
#define    PIO_PIN_PC27     (91U)
#define    PIO_PIN_PC28     (92U)
#define    PIO_PIN_PC29     (93U)
#define    PIO_PIN_PC30     (94U)
#define    PIO_PIN_PC31     (95U)
#define    PIO_PIN_PD0     (96U)
#define    PIO_PIN_PD1     (97U)
#define    PIO_PIN_PD2     (98U)
#define    PIO_PIN_PD3     (99U)
#define    PIO_PIN_PD4     (100U)
#define    PIO_PIN_PD5     (101U)
#define    PIO_PIN_PD6     (102U)
#define    PIO_PIN_PD7     (103U)
#define    PIO_PIN_PD8     (104U)
#define    PIO_PIN_PD9     (105U)
#define    PIO_PIN_PD10     (106U)
#define    PIO_PIN_PD11     (107U)
#define    PIO_PIN_PD12     (108U)
#define    PIO_PIN_PD13     (109U)
#define    PIO_PIN_PD14     (110U)
#define    PIO_PIN_PD15     (111U)
#define    PIO_PIN_PD16     (112U)
#define    PIO_PIN_PD17     (113U)
#define    PIO_PIN_PD18     (114U)
#define    PIO_PIN_PD19     (115U)
#define    PIO_PIN_PD20     (116U)
#define    PIO_PIN_PD21     (117U)

    /* This element should not be used in any of the PIO APIs.
       It will be used by other modules or application to denote that none of the PIO Pin is used */
#define    PIO_PIN_NONE         ( -1)

typedef uint32_t PIO_PIN;


void PIO_Initialize(void);

// *****************************************************************************
// *****************************************************************************
// Section: PIO Functions which operates on multiple pins of a port
// *****************************************************************************
// *****************************************************************************

uint32_t PIO_PortRead(PIO_PORT port);

void PIO_PortWrite(PIO_PORT port, uint32_t mask, uint32_t value);

uint32_t PIO_PortLatchRead ( PIO_PORT port );

void PIO_PortSet(PIO_PORT port, uint32_t mask);

void PIO_PortClear(PIO_PORT port, uint32_t mask);

void PIO_PortToggle(PIO_PORT port, uint32_t mask);

void PIO_PortInputEnable(PIO_PORT port, uint32_t mask);

void PIO_PortOutputEnable(PIO_PORT port, uint32_t mask);

// *****************************************************************************
// *****************************************************************************
// Section: PIO Functions which operates on one pin at a time
// *****************************************************************************
// *****************************************************************************

static inline void PIO_PinWrite(PIO_PIN pin, bool value)
{
    PIO_PortWrite((PIO_PORT)(PIOA_BASE_ADDRESS + (0x200U * (pin>>5U))), (uint32_t)(0x1) << (pin & 0x1fU), (uint32_t)(value) << (pin & 0x1fU));
}

static inline bool PIO_PinRead(PIO_PIN pin)
{
    return (bool)((PIO_PortRead((PIO_PORT)(PIOA_BASE_ADDRESS + (0x200U * (pin>>5U)))) >> (pin & 0x1FU)) & 0x1U);
}

static inline bool PIO_PinLatchRead(PIO_PIN pin)
{
    return (bool)((PIO_PortLatchRead((PIO_PORT)(PIOA_BASE_ADDRESS + (0x200U * (pin>>5U)))) >> (pin & 0x1FU)) & 0x1U);
}

static inline void PIO_PinToggle(PIO_PIN pin)
{
    PIO_PortToggle((PIO_PORT)(PIOA_BASE_ADDRESS + (0x200U * (pin>>5U))), 0x1UL << (pin & 0x1FU));
}

static inline void PIO_PinSet(PIO_PIN pin)
{
    PIO_PortSet((PIO_PORT)(PIOA_BASE_ADDRESS + (0x200U * (pin>>5))), 0x1UL << (pin & 0x1FU));
}

static inline void PIO_PinClear(PIO_PIN pin)
{
    PIO_PortClear((PIO_PORT)(PIOA_BASE_ADDRESS + (0x200U * (pin>>5U))), 0x1UL << (pin & 0x1FU));
}

static inline void PIO_PinInputEnable(PIO_PIN pin)
{
    PIO_PortInputEnable((PIO_PORT)(PIOA_BASE_ADDRESS + (0x200U * (pin>>5U))), 0x1UL << (pin & 0x1FU));
}

static inline void PIO_PinOutputEnable(PIO_PIN pin)
{
    PIO_PortOutputEnable((PIO_PORT)(PIOA_BASE_ADDRESS + (0x200U * (pin>>5U))), 0x1UL << (pin & 0x1FU));
}


// DOM-IGNORE-BEGIN
#ifdef __cplusplus  // Provide C++ Compatibility

    }

#endif
// DOM-IGNORE-END
#endif // PLIB_PIO_H
